﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework;
using Ue.Qcdz.Domain.TreeViews;
using TreeNode = Ue.Qcdz.Domain.TreeViews.TreeNode;
using TreeView = Ue.Qcdz.Domain.TreeViews.TreeView;


namespace Ue.Qcdz.UI.Controls
{
    public class TreeNodeGridView : Ue.Infrastructure.Web.UI.SkinnedGridView
    {
        private ITreeViewRepository treeViewRepository;

        public TreeNodeGridView()
        {
            this.treeViewRepository = RepositoryFactory.GetRepository<ITreeViewRepository, TreeView>();
        }

        protected override void GridViewDataBind(object sender, EventArgs args)
        {
            Repeater repeater = sender as Repeater;

            TreeView treeView = this.treeViewRepository.FindBy(this.TreeViewId);
            if(treeView==null)
            {
                return;
            }

            IEnumerable<TreeNode> childNodes = treeView.ChildNodes;

            if(TreeNodeId!=null)
            {
                TreeNode treeNode = treeView.FindNodeBy(this.TreeNodeId);
                if (treeNode!=null)
                {
                    childNodes = treeNode.ChildNodes;
                }
                else
                {
                    childNodes = Enumerable.Empty<TreeNode>();
                }
            }

            this.RecordCount = childNodes.Count();

            repeater.DataSource = childNodes;
            repeater.DataBind();
        }

        public object TreeViewId { get; set; }

        public object TreeNodeId { get; set; }

        protected override string GetSkinFileName()
        {
            return string.Empty;
        }
    }
}
